﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Utils.DC.Web.UI
{
    /// <summary>
    /// Class chua cac ham bao mat, nhu encode va decode
    /// </summary>
    public class Security
    {
        /// <summary>
        /// Ham dung de ma hoa mat khau
        /// </summary>
        /// <param name="cleanString"></param>
        /// <returns></returns>
        public static string Encrypt(string cleanString)
        {
            byte[] clearBytes;
            byte[] hashedBytes;

            clearBytes = new UnicodeEncoding().GetBytes(cleanString);
            hashedBytes = ((HashAlgorithm)(CryptoConfig.CreateFromName("MD5"))).ComputeHash(clearBytes);
            return BitConverter.ToString(hashedBytes);
        }

        #region Cac Bien Dung Trong Ma Hoa

        private static readonly byte[] KEY = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
        private static readonly byte[] IV = { 65, 110, 68, 26, 69, 178, 200, 219 };
        private static readonly string[] SKEY = { "A", "a", "B", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "Z", "z", "X", "x", "Y", "y", "W", "w", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "\'", "<", ">", "?", "/", "\\", "|" };
        private static readonly string TKEY = "AaBcDdEeFfGgHhIiKkLlMmNnOoPpQqRrSsTtZzXxYyWw!@#$%^&*()-_+=\'<>?/\\|";
        private static readonly int TimeDelay = 8;
        private static readonly int DEFAULT_MIN_PASSWORD_LENGTH = 8;
        private static readonly int DEFAULT_MAX_PASSWORD_LENGTH = 10;
        private static readonly string PASSWORD_CHARS_LCASE = "abcdefgijkmnopqrstwxyz";
        private static readonly string PASSWORD_CHARS_UCASE = "ABCDEFGHJKLMNPQRSTWXYZ";
        private static readonly string PASSWORD_CHARS_NUMERIC = "23456789";
        private static readonly string PASSWORD_CHARS_SPECIAL = "*$-+?_&=!%{}/";

        #endregion 

        /// <summary>
        /// Giải mã
        /// </summary>
        /// <param name="value">Xâu Ký Tự - Văn Bản đã được mã hóa từ trước</param>
        /// <returns>Xâu Ký Tự - Văn Bản đã được Giải mã</returns>
        /// <remarks></remarks>
        public static string Decode(string value)
        {
            var des = new TripleDESCryptoServiceProvider();
            des.IV = IV;
            var pdb = new PasswordDeriveBytes(TKEY, new byte[-1 + 1]);
            des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
            byte[] encryptedBytes = Convert.FromBase64String(value);
            var ms = new MemoryStream(value.Length);
            var decStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
            decStream.FlushFinalBlock();
            byte[] plainBytes = new byte[Convert.ToInt32(ms.Length - 1) + 1];
            ms.Position = 0;
            ms.Read(plainBytes, 0, (int)ms.Length);
            decStream.Close();
            return System.Text.Encoding.UTF8.GetString(plainBytes);
        }
    }
}
